﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    /// <summary>
    /// 课表管理
    /// </summary>
    public class TimeTableController : BaseController
    {
        private readonly ITimeTableService _timeTableService;
        private readonly IClazzService _clazzService;
        private readonly ICourceService _courceService;
        private readonly IUserService _userService;

        public TimeTableController(ITimeTableService timeTableService, IClazzService clazzService, ICourceService courceService, IUserService userService)
        {
            _timeTableService = timeTableService;
            _clazzService = clazzService;
            _courceService = courceService;
            _userService = userService;
        }

        // GET: Form
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增课表
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 课表所属课程列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CourceList(long id)
        {
            ViewData["id"] = id;
            return View();
        }
        /// <summary>
        /// 课表所属课程列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CourceList4Stu()
        {
            ViewData["id"] = 0;
            return View();
        }

        /// <summary>
        /// 添加课程信息到对应课表
        /// </summary>
        /// <param name="id">课表ID</param>
        /// <returns></returns>
        public ActionResult CreateCource(long id)
        {
            ViewData["id"] = id;
            return View();
        }

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="tpVO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateCource(TimeTableProjectEntity tpVO)
        {
            try
            {
                if (tpVO.StartTime.Ticks == 0)
                {
                    throw new Exception("请选择开始时间！");
                }
                if (tpVO.StopTime.Ticks == 0)
                {
                    throw new Exception("请选择结束时间！");
                }
                if (tpVO.Address.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入地点！");
                }

                long id = _timeTableService.CreateCource(tpVO);
                return Content(id.ToString());
            }
            catch (Exception ex)
            {
                return Content("err:" + ex.Message);
            }
        }

        /// <summary>
        /// 创建课表
        /// </summary>
        /// <param name="timeTableVO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(TimeTableEntity timeTableVO)
        {
            try
            {
                if (timeTableVO.TableName.IsNullOrWhiteSpace())
                {
                    throw new Exception( "课表名称不能为空！");
                }
                if (timeTableVO.ClazzId == 0)
                {
                    throw new Exception("所属班级不能为空！");
                }
                timeTableVO.CreateTime = DateTime.Now;
                timeTableVO = _timeTableService.Add(timeTableVO);
                return Content(timeTableVO.Id.ToString());
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        /// <summary>
        /// 编辑课表页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            ViewBag.TimeTableId = id;
            return View();
        }

        [HttpGet]
        public ActionResult GetTimeTableInfo(long id)
        {
            var timeTable = _timeTableService.Find(id);
            return Json(timeTable, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建课表
        /// </summary>
        /// <param name="timeTableVO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(TimeTableEntity timeTableVO)
        {
            try
            {
                if (timeTableVO.TableName.IsNullOrWhiteSpace())
                {
                    throw new ArgumentNullException(nameof(timeTableVO.TableName), "课表名称不能为空！");
                }
                if (timeTableVO.ClazzId == 0)
                {
                    throw new ArgumentNullException(nameof(timeTableVO.ClazzId), "所属班级不能为空！");
                }
                TimeTableEntity timeTableFromDb = _timeTableService.Find(timeTableVO.Id);
                timeTableFromDb.TableName = timeTableVO.TableName;
                timeTableFromDb.Remark = timeTableVO.Remark;
                timeTableFromDb.ClazzId = timeTableVO.ClazzId;
                // timeTableVO.CreateTime = DateTime.Now;
                _timeTableService.Update(timeTableFromDb);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        [HttpPost]
        public ActionResult Delete(long timeTableId)
        {
            _timeTableService.Delete(timeTableId);
            return Content("true");
        }

        /// <summary>
        /// 课表列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {

            return View();
        }


        /// <summary>
        /// 课表列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List4Stu()
        {

            return View();
        }

        /// <summary>
        /// 课表列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllTimeTableList()
        {
            return Json(_timeTableService.GetTimeTableList(), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 课表列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllTimeTableList4Stu()
        {
            return Json(_timeTableService.GetTimeTableListByStuId(CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据课表ID获取课程列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetCourceList()
        {
            long id = 0;
            var timeTableList = _timeTableService.GetTimeTableListByStuId(CurrentUser.UserId);
            if (timeTableList.Any())
            {
                id = timeTableList.FirstOrDefault().Id;
            }
            return Json(_timeTableService.GetTimeTableCourceList(id), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据课表ID获取课程列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetCourceList4Teacher(long id)
        {
            return Json(_timeTableService.GetTimeTableCourceList(id), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取课程列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetAllCourceList()
        {
            return Json(_courceService.LoadEntities(), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据课表ID获取课程列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CancelProject(long projectId)
        {
            _timeTableService.CancelProject(projectId);
            return Content("true");
        }

        /// <summary>
        /// 获取教师列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetTeacherList()
        {
            return Json(_userService.LoadEntities(x => x.UserType == 2), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllClazzEntity()
        {
            return Json(_clazzService.LoadClazzByUserId(CurrentUser.UserId, 2), JsonRequestBehavior.AllowGet);
        }
    }
}